﻿using ElectronicShoppingMall.Service.Data;
using ElectronicShoppingMall.Service.Models.Enterprise;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ElectronicShoppingMall.Service.Services.Enterprise
{
    public class OrderService: IDisposable
    {
        private ElectronicShoppingMallContext data;
        public OrderService()
        {
            data = new ElectronicShoppingMallContext();
        }


        public void Dispose()
        {
            data.Dispose();
        }
        /// <summary>
        ///获取所有未发货的订单
        /// </summary>
        /// <returns></returns>
        public List<OrderModel> GetAllOrdersNotShipped()
        {
            List<OrderModel> result = new List<OrderModel>();
            var orders = data.Orders.Where(o => o.State == 0);
            foreach(var order in orders)
            {
                OrderModel temp = new OrderModel()
                {
                    OrderId = order.OrderId,
                    DeliveryAddress = order.DeliveryAddress,
                    ClientId = order.ClientId,
                    Consignee = order.Consignee,
                    Count = order.Count,
                    CreateTime = order.CreateTime.ToString(),
                    State = order.State,
                    DeliveryTime = order.DeliveryTime.ToString(),
                    GoodsName = order.GoodsName,
                    IsDelete = order.IsDelete,
                    PaymentTime = order.PaymentTime.ToString(),
                    ConsigneePhoneNumber = order.PhoneNumber,
                    Price = order.Price,
                    Total = order.Price * order.Count
                };
                result.Add(temp);
            }
            return result;
        }
        /// <summary>
        /// 获取未确认发货的订单
        /// </summary>
        /// <returns></returns>
        public List<OrderModel> GetUnconfirmedDeliveryOrder()
        {
            List<OrderModel> result = new List<OrderModel>();
            var orders = data.Orders.Where(o => o.State == 1);
            foreach(var order in orders)
            {
                OrderModel temp = new OrderModel()
                {
                    OrderId = order.OrderId,
                    DeliveryAddress = order.DeliveryAddress,
                    ClientId = order.ClientId,
                    Consignee = order.Consignee,
                    Count = order.Count,
                    CreateTime = order.CreateTime.ToString(),
                    State = order.State,
                    DeliveryTime = order.DeliveryTime.ToString(),
                    GoodsName = order.GoodsName,
                    IsDelete = order.IsDelete,
                    PaymentTime = order.PaymentTime.ToString(),
                    ConsigneePhoneNumber = order.PhoneNumber,
                    Price = order.Price,
                    Total = order.Price * order.Count
                };
                result.Add(temp);
            }
            return result;
        }
        /// <summary>
        /// 处理退货的订单
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="operation"></param>
        public void ProcessingOrdersForReturn(string orderId, string operation)
        {
            if (operation != "agree" &&operation != "refuse")
            {
                throw new Exception("非法操作");
            }
            var order = data.Orders.SingleOrDefault(o => o.OrderId == orderId);
            if (operation == "agree")
            {
                //状态为4则表示同意退货
                order.State = 4;
            }
            else
            {
                //状态为1则表示拒绝退货
                order.State = 1;
            }
            data.SaveChanges();
        }
        /// <summary>
        /// 获取退款的订单
        /// </summary>
        /// <returns></returns>
        public List<OrderModel> GetRefundOrders()
        {
            List<OrderModel> result = new List<OrderModel>();
            //申请退款的订单的状态为6
            var orders = data.Orders.Where(o => o.State == 6);
            foreach (var order in orders)
            {
                OrderModel temp = new OrderModel()
                {
                    OrderId = order.OrderId,
                    ClientId = order.ClientId,
                    Count = order.Count,
                    CreateTime = order.CreateTime.ToString(),
                    State = order.State,
                    GoodsName = order.GoodsName,
                    IsDelete = order.IsDelete,
                    PaymentTime = order.PaymentTime.ToString(),
                    Price = order.Price,
                    Total = order.Price * order.Count,
                    ReasonForApplication = order.ReasonForApplication,
                    ApplicationTime = order.ApplicationTime.ToString()
                };
                var user = data.Clients.SingleOrDefault(c => c.ClientId == order.ClientId).User;
                temp.ClientName = user.NickName;
                temp.ClientPhoneNumber = user.PhoneNumber;
                result.Add(temp);
            }
            return result;
        }
        /// <summary>
        /// 处理退款订单
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="operation"></param>
        public void ProcessingOrdersForRefund(string  orderId, string operation)
        {
            if (operation != "agree" && operation != "refuse")
            {
                throw new Exception("非法操作");
            }
            var order = data.Orders.SingleOrDefault(o => o.OrderId == orderId);
            if (operation == "agree")
            {
                //状态为7则表示同意退款并且修改库存
                var goods = data.GoodsInfoes.SingleOrDefault(g => g.GoodsId == order.GoodsId);
                goods.Stock += order.Count;
                order.State = 7;
            }
            else
            {
                //状态为2则表示拒绝退款
                order.State = 2;
            }
            data.SaveChanges();
        }
        /// <summary>
        /// 获取申请退款的订单
        /// </summary>
        /// <returns></returns>
        public List<OrderModel> GetReturnOrder()
        {
            List<OrderModel> result = new List<OrderModel>();
            //申请退货的订单的状态为3
            var orders = data.Orders.Where(o => o.State == 3);
            foreach (var order in orders)
            {
                OrderModel temp = new OrderModel()
                {
                    OrderId = order.OrderId,
                    ClientId = order.ClientId,
                    Count = order.Count,
                    CreateTime = order.CreateTime.ToString(),
                    State = order.State,
                    GoodsName = order.GoodsName,
                    IsDelete = order.IsDelete,
                    PaymentTime = order.PaymentTime.ToString(),
                    Price = order.Price,
                    Total = order.Price * order.Count,
                    ReasonForApplication = order.ReasonForApplication,
                    ApplicationTime = order.ApplicationTime.ToString()
                };
                var user = data.Clients.SingleOrDefault(c => c.ClientId == order.ClientId).User;
                temp.ClientName = user.NickName;
                temp.ClientPhoneNumber = user.PhoneNumber;
                result.Add(temp);
            }
            return result;
        }
        /// <summary>
        /// 确认发货
        /// </summary>
        /// <param name="orderId"></param>
        public void ConfirmationOfDelivery(string orderId)
        {
            //已确认发货的订单状态为2
            var order = data.Orders.SingleOrDefault(o => o.OrderId == orderId);
            order.State = 2;
            data.SaveChanges();
        }

        /// <summary>
        /// 商品发货
        /// </summary>
        /// <param name="goodsId"></param>
        public void DeliverGoods(string orderId)
        {
            //已发货的订单状态为1
            var order = data.Orders.SingleOrDefault(o => o.OrderId == orderId);
            order.State = 1;
            data.SaveChanges();
        }
    }
}
